// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/common/meta_programming.h>
#include <pollux/type/simple_function_api.h>
#include <pollux/type/type.h>

#define POLLUX_DEFINE_FUNCTION_TYPES(__Pollux_ExecParams)                 \
  template <typename TArgs>                                             \
  using arg_type =                                                      \
      typename __Pollux_ExecParams::template resolver<TArgs>::in_type;   \
                                                                        \
  template <typename TArgs>                                             \
  using out_type =                                                      \
      typename __Pollux_ExecParams::template resolver<TArgs>::out_type;  \
                                                                        \
  template <typename TArgs>                                             \
  using opt_arg_type = std::optional<                                   \
      typename __Pollux_ExecParams::template resolver<TArgs>::in_type>;  \
                                                                        \
  template <typename TArgs>                                             \
  using opt_out_type = std::optional<                                   \
      typename __Pollux_ExecParams::template resolver<TArgs>::out_type>; \
                                                                        \
  DECLARE_CONDITIONAL_TYPE_NAME(                                        \
      null_free_in_type_resolver, null_free_in_type, in_type);          \
  template <typename TArgs>                                             \
  using null_free_arg_type =                                            \
      typename null_free_in_type_resolver::template resolve<            \
          typename __Pollux_ExecParams::template resolver<TArgs>>::type; \
                                                                        \
  template <typename TKey, typename TVal>                               \
  using MapVal = arg_type<::kumo::pollux::Map<TKey, TVal>>;          \
  template <typename TElement>                                          \
  using ArrayVal = arg_type<::kumo::pollux::Array<TElement>>;        \
  using VarcharVal = arg_type<::kumo::pollux::Varchar>;              \
  using VarbinaryVal = arg_type<::kumo::pollux::Varbinary>;          \
  template <typename... TArgss>                                         \
  using RowVal = arg_type<::kumo::pollux::Row<TArgss...>>;           \
  template <typename TKey, typename TVal>                               \
  using MapWriter = out_type<::kumo::pollux::Map<TKey, TVal>>;       \
  template <typename TElement>                                          \
  using ArrayWriter = out_type<::kumo::pollux::Array<TElement>>;     \
  using VarcharWriter = out_type<::kumo::pollux::Varchar>;           \
  using VarbinaryWriter = out_type<::kumo::pollux::Varbinary>;       \
  template <typename... TArgss>                                         \
  using RowWriter = out_type<::kumo::pollux::Row<TArgss...>>;

#define POLLUX_UDF_BEGIN(Name)                         \
  struct udf_##Name {                                 \
    static constexpr auto name = #Name;               \
    template <typename __Pollux_ExecParams>            \
    struct udf {                                      \
      POLLUX_DEFINE_FUNCTION_TYPES(__Pollux_ExecParams) \
      static constexpr auto name = #Name;

#define POLLUX_UDF_END() \
  }                     \
  ;                     \
  }                     \
  ;
